//
//  thirdviewController.m
//  UI练习
//
//  Created by dllo on 16/3/19.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "thirdviewController.h"
#import "MtTextField.h"
#import "MyButton.h"
#import "Mylabel.h"
#import "MySlider.h"
#import "MySwitch.h"
#import "UIImageViewOfAnimal.h"
@interface thirdviewController ()

@property (nonatomic, retain) UIImageViewOfAnimal *image;
@property (nonatomic, retain) MySwitch *switchOfOn;
@property (nonatomic, retain) UISegmentedControl *segment;
@property (nonatomic, retain) MySlider *slider;
@property (nonatomic ,retain) MyButton *button;
@property (nonatomic, retain) UIImageViewOfAnimal *image1;
@end

@implementation thirdviewController
- (void)dealloc{
    [_image release];
    [_switchOfOn release];
    [_segment release];
    [_slider release];
    [_button release];
    [_image1 release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createNavigation];
    [self createlabel];
    [self createImage];
    [self createSwitch];
    [self createSlider];
    [self createSegment];
    [self createButton];
    [self createSwitch1];

}


#pragma mark - 创建bar并在右边添加符号和点击事件,实现点击事件
- (void)createNavigation{
    
    self.navigationItem.title = @"僵尸动画";
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"iconfont-xiangyou1.png"] style:UIBarButtonItemStylePlain target:self action:@selector(handleRight:)];
    [self.navigationItem.rightBarButtonItem release];
    
}
- (void)handleRight:(UIBarButtonItem *)right{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
#pragma mark - 创建label
- (void)createlabel{
    Mylabel *label = [[Mylabel alloc]initWithFrame:CGRectMake(138, 100, 200, 50) text:@"僵尸漫画"];
    [self.view addSubview:label];
}
- (void)createImage{
    self.image = [[UIImageViewOfAnimal alloc]initWithImage:[UIImage imageNamed:@"BZombie1.tiff"]];
    
    self.image.frame = CGRectMake(80, 150, 300, 400);
    [self.view addSubview:self.image];
    
    self.image1 = [[UIImageViewOfAnimal alloc]initWithImage:[UIImage imageNamed:@"Zombie0.tiff"]];
    self.image1.frame = CGRectMake(-50, 150, 300, 400);
    [self.view addSubview:self.image1];
    [self.image1 release];
}
#pragma mark - 创建开关和实现动画以及实现开关的点击事件
- (void)createSwitch{
    self.switchOfOn = [[MySwitch alloc]initWithFrame:CGRectMake(300, 600, 100, 50)];
    self.switchOfOn.thumbTintColor = [UIColor cyanColor];
    [self.view addSubview:self.switchOfOn];
    [self.switchOfOn addTarget:self action:@selector(handleSwitch:) forControlEvents:UIControlEventValueChanged];
    [self.switchOfOn release];
}
- (void)handleSwitch:(UISwitch *)Swi{
    NSMutableArray *mArr = [NSMutableArray array];
    if (Swi.on == YES) {
        for (int i = 0; i < 24; i++) {
            NSString *name = [NSString stringWithFormat:@"BZombie%d.tiff", i+1];
            UIImage *image = [UIImage imageNamed:name];
            [mArr addObject:image];
        }
        
        self.image.animationDuration = 5;
        self.image.animationImages = mArr;
        self.image.animationRepeatCount = 0;
        [self.image startAnimating];
    }else{
        [self.image stopAnimating];
        [mArr removeAllObjects];
    }
    
    
}
- (void)createSwitch1{
    self.switchOfOn = [[MySwitch alloc]initWithFrame:CGRectMake(300, 650, 100, 50)];
    self.switchOfOn.thumbTintColor = [UIColor cyanColor];
    [self.view addSubview:self.switchOfOn];
    [self.switchOfOn addTarget:self action:@selector(handleSwitch1:) forControlEvents:UIControlEventValueChanged];
    [self.switchOfOn release];
}
- (void)handleSwitch1:(UISwitch *)Swi{
    NSMutableArray *mArr1 = [NSMutableArray array];
    if (Swi.on == YES) {
        for (int i = 0; i < 20; i++) {
            NSString *name = [NSString stringWithFormat:@"Zombie%d.tiff", i+1];
            UIImage *image = [UIImage imageNamed:name];
            [mArr1 addObject:image];
        }
        
        self.image1.animationDuration = 5;
        self.image1.animationImages = mArr1;
        self.image1.animationRepeatCount = 0;
        [self.image1 startAnimating];
    }else{
        [self.image1 stopAnimating];
        [mArr1 removeAllObjects];
    }
    
    
}

#pragma mark - 创建slider控制动画的速度
- (void)createSlider{
    self.slider = [[MySlider alloc]initWithFrame:CGRectMake(40, 590, 250, 50)];
    [self.view addSubview:self.slider];
    [_slider release];
    [self.slider addTarget: self action:@selector(handleSlider:) forControlEvents:UIControlEventValueChanged];
    //为进度条赋值
    self.slider.maximumValue = 10;
    self.slider.minimumValue = 1;
    
}
- (void)handleSlider:(UISlider *)sli{
    if (self.switchOfOn.on) {
        self.image.animationDuration = 10 - self.slider.value;
        [self.image startAnimating];
        self.image1.animationDuration = 10 - self.slider.value;
        [self.image1 startAnimating];
    }
    
}
#pragma mark - 创建分段控制
- (void)createSegment{
    self.segment = [[UISegmentedControl alloc]initWithFrame:CGRectMake(130, 80, 300, 50)];
    [self.view addSubview:self.segment];
    [self.segment initWithItems:@[@"夜间", @"白天" ,@"护眼"]];
    [_segment release];
    [self.segment addTarget:self action:@selector(handleSegment:) forControlEvents:UIControlEventValueChanged];
}
- (void)handleSegment:(UISegmentedControl *)seg{
    switch (seg.selectedSegmentIndex) {
        case 0:
            self.view.backgroundColor = [UIColor blackColor];
            break;
        case 1:
            self.view.backgroundColor = [UIColor cyanColor];
            break;
        case 2:
            self.view.backgroundColor = [UIColor colorWithRed:0.1 green:0.9 blue:0.6 alpha:0.5];
            break;
        default:
            break;
    }
}
#pragma mark - 创建按钮跳到海贼王漫画.
- (void)createButton{
    self.button = [MyButton buttonWithType:UIButtonTypeSystem];
    self.button.frame = CGRectMake(150, 650, 100, 50);
    [self.button setTitle:@"首页" forState:UIControlStateNormal];
    [self.view addSubview:self.button];
    self.button.titleLabel.font = [UIFont systemFontOfSize:25];
    self.button.tintColor = [UIColor redColor];
    [self.button addTarget:self action:@selector(handleButton:) forControlEvents:UIControlEventTouchUpInside];
    
}
- (void)handleButton:(UIButton *)button{
    [self.navigationController popToRootViewControllerAnimated:YES];

}

















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
